package com.isdepci.project.carbon.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.EmissionPathLog;
import com.isdepci.project.carbon.domain.ProductCarbon;
import com.isdepci.project.carbon.mapper.EmissionPathLogMapper;
import com.isdepci.project.carbon.mapper.ProductCarbonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.isdepci.project.carbon.mapper.ProductCarbonConsumablesMapper;
import com.isdepci.project.carbon.domain.ProductCarbonConsumables;
import com.isdepci.project.carbon.service.IProductCarbonConsumablesService;

/**
 * 产品碳足迹-产品排放行为记录Service业务层处理
 *
 * @author isdepci
 * @date 2024-11-11
 */
@Service
public class ProductCarbonConsumablesServiceImpl implements IProductCarbonConsumablesService {
    @Autowired
    private ProductCarbonConsumablesMapper productCarbonConsumablesMapper;
    @Autowired
    private ProductCarbonMapper productCarbonMapper;
    @Autowired
    private EmissionPathLogMapper emissionPathLogMapper;

    @Override
    public BigDecimal selectAvgByProductId(Long productId) {
        List<ProductCarbonConsumables> list = productCarbonConsumablesMapper.selectConsumablesListByProductId(productId);
        if (list.size() <= 0) {
            return BigDecimal.ZERO;
        }
        List<BigDecimal> nums = list.stream().filter(v -> v.getFactorNum()!=null && v.getFactorNum().compareTo(BigDecimal.ZERO) > 0
                && v.getQuantity()!=null &&  v.getQuantity().compareTo(BigDecimal.ZERO) > 0)
                .map(v -> v.getFactorNum().multiply(v.getQuantity())).collect(Collectors.toList());
        if (nums.size() <= 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal sum = nums.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        return sum.divide(new BigDecimal(nums.size()), 8, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public AjaxResult selectEmissionPath(Long id) {
        ProductCarbonConsumables old = productCarbonConsumablesMapper.selectProductCarbonConsumablesById(id);
        if(old==null){
            return AjaxResult.error("未找到相关信息！");
        }
        /**
         * 查询减排路径
         *    产品名称、碳排放行为相同
         *    碳排放因子低于当前因子数值
         *    从小到大排列
         */
        List<ProductCarbonConsumables> list = productCarbonConsumablesMapper.selectEmissionPath(old.getProductId(),old.getCategorizeCode(),old.getFactorNum());
        if(list.size()>0){
            List<ProductCarbon> carbons = productCarbonMapper.selectListByIds(list.stream().map(ProductCarbonConsumables::getProductCarbonId).collect(Collectors.toList()));
            for (ProductCarbonConsumables consumables : list) {
                ProductCarbon carbon = carbons.stream().filter(v-> v.getId().equals(consumables.getProductCarbonId())).findFirst().orElse(null);
                if(carbon!=null){
                    consumables.setEnterpriseId(carbon.getEnterpriseId());
                    consumables.setProductBatch(carbon.getProductBatch());
                }
            }
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult summaryEmissionPathLog(Long id, List<Long> oldIds) {
        ProductCarbonConsumables old = productCarbonConsumablesMapper.selectProductCarbonConsumablesById(id);
        if(old==null){
            return AjaxResult.error("未找到相关信息！");
        }
        ProductCarbon carbon = productCarbonMapper.selectProductCarbonById(old.getProductCarbonId());
        if(carbon==null){
            return AjaxResult.error("未找到批次相关信息！");
        }
        List<ProductCarbonConsumables> list = productCarbonConsumablesMapper.selectProductCarbonConsumablesByIds(oldIds);
        if(list==null||list.size()<=0){
            return AjaxResult.error("未找到采纳的因子信息！");
        }
        List<EmissionPathLog> inList = new ArrayList<>();
        for (ProductCarbonConsumables consumables : list) {
            inList.add(new EmissionPathLog(carbon,old,consumables));

        }
        if(inList.size()>0){
            emissionPathLogMapper.insertList(inList);
        }
        return AjaxResult.success();
    }

    @Override
    public List<ProductCarbonConsumables> selectEmissionPathList(ProductCarbonConsumables param) {
        return productCarbonConsumablesMapper.selectEmissionPathList(param);
    }

    /**
     * 查询产品碳足迹-产品排放行为记录
     */
    @Override
    public ProductCarbonConsumables selectProductCarbonConsumablesById(Long id) {
        return productCarbonConsumablesMapper.selectProductCarbonConsumablesById(id);
    }

    /**
     * 查询产品碳足迹-产品排放行为记录列表
     */
    @Override
    public List<ProductCarbonConsumables> selectProductCarbonConsumablesList(ProductCarbonConsumables productCarbonConsumables) {
        return productCarbonConsumablesMapper.selectProductCarbonConsumablesList(productCarbonConsumables);
    }

    /**
     * 新增产品碳足迹-产品排放行为记录
     */
    @Override
    public int insertProductCarbonConsumables(ProductCarbonConsumables productCarbonConsumables) {
        return productCarbonConsumablesMapper.insertProductCarbonConsumables(productCarbonConsumables);
    }

    /**
     * 修改产品碳足迹-产品排放行为记录
     */
    @Override
    public int updateProductCarbonConsumables(ProductCarbonConsumables productCarbonConsumables) {
        return productCarbonConsumablesMapper.updateProductCarbonConsumables(productCarbonConsumables);
    }

    /**
     * 批量删除产品碳足迹-产品排放行为记录
     */
    @Override
    public int deleteProductCarbonConsumablesByIds(Long[] ids) {
        return productCarbonConsumablesMapper.deleteProductCarbonConsumablesByIds(ids);
    }

    /**
     * 删除产品碳足迹-产品排放行为记录信息
     */
    @Override
    public int deleteProductCarbonConsumablesById(Long id) {
        return productCarbonConsumablesMapper.deleteProductCarbonConsumablesById(id);
    }


}
